package com.tanhua.admin.interceptor;

import com.tanhua.admin.common.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 拦截器的注册
 * 实现WebMvcConfigurer
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")  //拦截所有的请求
                .excludePathPatterns(new String[]{"/system/users/login", "/system/users/verification"});
    }

    /**
     * 配置消息转换器
     *
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建消息转换器
        MappingJackson2HttpMessageConverter m2jhm = new MappingJackson2HttpMessageConverter();
        //设置映射
        m2jhm.setObjectMapper(new JacksonObjectMapper());
        //添加到集合中0号元素，让他优先使用
        converters.add(0, m2jhm);
    }
}
